package ejb.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import ejb.entity.DBImage;

@Stateless
public class DefaultDBImageDAO implements ImageDAO {
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public DBImage create(DBImage img){
		try{
			em.persist(img);
		}catch(IllegalArgumentException e){
			return null;
		}
		return img;
	}
	
	@Override
	public DBImage getImageByName(String name){
		try{
			DBImage image = (DBImage) em.createNamedQuery("DefaultDBImage.findByName").setParameter("name", name).getSingleResult();
			return image;
		}catch(NoResultException nre){
			return null;
		}catch(IllegalArgumentException e){
			return null;
		}
	}
	
	@Override
	public boolean delete(DBImage image) {
		try{
			em.remove(image);
		}catch(IllegalArgumentException e){
			return false;
		}
		return true;
	}
}
